package hall

import (
	"context"
	"io"
	"net/http"
)

/*
Handler functions
*/

func (f *FuncHandler[Req, Resp]) Handle(ctx context.Context, req Req, resp *Resp) {
	f.HandlerFunc(ctx, req, resp)
}

func (f *FuncHandler[Req, Resp]) ReqDecoder(ctx context.Context, reader io.Reader, req *Req) error {
	return f.Decoder(ctx, reader, req)
}

func (f *FuncHandler[Req, Resp]) RespEncoder(ctx context.Context, resp Resp, writer io.Writer) error {
	return f.Encoder(ctx, resp, writer)
}

/*
UserParam functions
*/

func NewUserParams() *UserParams {
	return &UserParams{
		params: make(map[string]string),
	}
}

func (u *UserParams) GetUserValue(key string) string {
	return u.params[key]
}

func (u *UserParams) SetUserValue(key, value string) {
	u.params[key] = value
}

func UserParamsFromContext(ctx context.Context) *UserParams {

	if ctx == nil {
		return nil
	}
	if params, ok := ctx.Value(UserParamsKey).(*UserParams); ok {
		return params
	}
	return nil

}

func RequestFromContext(ctx context.Context) *http.Request {

	if ctx == nil {
		return nil
	}
	if request, ok := ctx.Value(RequestKey).(*http.Request); ok {
		return request
	}
	return nil

}
